﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PicStore.Common.Sorter
{
    public class SortList<T, TKey> : List<T>
    {
        public SortList(IList<T> source, Expression<Func<T, TKey>> keySelector, bool isAsc)
        {
            if (isAsc)
                this.AddRange(source.OrderBy(keySelector.Compile()).ToList());
            else
                this.AddRange(source.OrderByDescending(keySelector.Compile()).ToList());
        }

        public SortList(IList<T> source, string columnName, bool isAsc)
        {
            if (isAsc)
                this.AddRange(source.OrderBy(new Func<T, object>(c => typeof(T).GetProperty(columnName).GetValue(c, null))));
            else
                this.AddRange(source.OrderByDescending(new Func<T, object>(c => typeof(T).GetProperty(columnName).GetValue(c, null))));
        }
    }
}
